package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ArrangeProgressBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ---安排行程
 */
public class ArrangeRouteActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    public static ArrangeProgressBean maxBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arrange_route);
        initData();
    }

    private String info_id;
    private String arrange_id;

    private void initData() {
        info_id = getIntent().getStringExtra("info_id");
        if (info_id == null) {
            return;
        }
        maxBean = new ArrangeProgressBean();
        maxBean.setCanUpdata("1");
        arrange_id = getIntent().getStringExtra("arrange_id");
        if (arrange_id == null) {
            initView();
        } else {
            if (!arrange_id.equals("0")) {
                getDetails(arrange_id);
            } else {
                initView();
            }
        }


    }

    private void getDetails(String arrange_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("arrange_id", arrange_id);
        map.put("info_id", info_id);
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_ARRANGETRIPDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        maxBean = gson.fromJson(info, ArrangeProgressBean.class);
                        maxBean.setCanUpdata("0");
                        initView();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("安排行程");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_address).setOnClickListener(this);//接客地址
        findViewById(R.id.rl_driver).setOnClickListener(this);//司机信息
        findViewById(R.id.rl_reception).setOnClickListener(this);//接待行程
        findViewById(R.id.rl_dinner).setOnClickListener(this);//用餐安排
        findViewById(R.id.rl_accommodation).setOnClickListener(this);//住宿安排
        findViewById(R.id.rl_remark).setOnClickListener(this);//备注

        re1 = (TextView) findViewById(R.id.re1);
        re2 = (TextView) findViewById(R.id.re2);
        re3 = (TextView) findViewById(R.id.re3);
        re4 = (TextView) findViewById(R.id.re4);
        re5 = (TextView) findViewById(R.id.re5);
        re6 = (TextView) findViewById(R.id.re6);

        if (maxBean.getTrip_list().size() != 0) {
            re1.setText("" + maxBean.getCustomer_address().getCity());
            re2.setText("" + maxBean.getDriver_info().getDriver_name());
            re3.setText("已选择" + maxBean.getTrip_list().size() + "处参观");
            re4.setText("" + maxBean.getRestaurant_info().getRes_name());
            re5.setText("" + maxBean.getHostel_info().getHotel_name());
            re6.setText("已填写");

        }
        if (maxBean.getCanUpdata().equals("1")) {
            findViewById(R.id.tv_submit).setOnClickListener(this);//提交
            findViewById(R.id.tv_submit).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.tv_submit).setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //接客地址
            case R.id.rl_address:
                intent = new Intent(this, GuestAddressActivity.class);
                startActivityForResult(intent, ARRCORD1);
                break;

            //司机信息
            case R.id.rl_driver:
                intent = new Intent(this, DriverMessageActivity.class);
                startActivityForResult(intent, ARRCORD2);
                break;

            //接待行程
            case R.id.rl_reception:
                intent = new Intent(this, ReceptionTravelActivity.class);
                startActivityForResult(intent, ARRCORD3);

                break;

            //用餐要求
            case R.id.rl_dinner:
                intent = new Intent(this, DinnerArrangementActivity.class);
                startActivityForResult(intent, ARRCORD4);
                break;

            //住宿安排
            case R.id.rl_accommodation:
                intent = new Intent(this, AccommodationActivity.class);
                startActivityForResult(intent, ARRCORD5);
                break;

            //备注
            case R.id.rl_remark:
                intent = new Intent(this, RemarksActivity.class);
                startActivityForResult(intent, ARRCORD6);
                break;

            //提交
            case R.id.tv_submit:
//                MyToast("提交");
                if ("".equals(re1.getText().toString().trim())) {
                    MyToast("请填写接客地址");
                    return;
                }
                if ("".equals(re2.getText().toString().trim())) {
                    MyToast("请填写司机信息");
                    return;
                }
                if ("".equals(re3.getText().toString().trim())) {
                    MyToast("请填写接待行程");
                    return;
                }
                if ("".equals(re4.getText().toString().trim())) {
                    MyToast("请填写用餐安排");
                    return;
                }
                if ("".equals(re5.getText().toString().trim())) {
                    MyToast("请填写住宿安排");
                    return;
                }
                if ("".equals(re6.getText().toString().trim())) {
                    MyToast("请填写备注");
                    return;
                }


                onUpLoad();
                break;


            default:
                break;
        }
    }

    private void onUpLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("info_id", info_id);
        map.put("all_info", JSON.toJSONString(maxBean));
        showDialog();
//        Set keys = map.keySet();
//        if (keys != null) {
//            Iterator iterator = keys.iterator();
//            while (iterator.hasNext()) {
//                Object key = iterator.next();
//                Object value = map.get(key);
//                Log.e("----" + key, ":" + value + "\n");
//            }
//        }
        XUtil.Post(Url.INFORMATION_ARRANGETRIP, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    public static final int ARRCORD1 = 11;
    public static final int ARRCORD2 = 12;
    public static final int ARRCORD3 = 13;
    public static final int ARRCORD4 = 14;
    public static final int ARRCORD5 = 15;
    public static final int ARRCORD6 = 16;
    private TextView re1, re2, re3, re4, re5, re6;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case ARRCORD1:
                    re1.setText("" + maxBean.getCustomer_address().getCity());
                    break;
                case ARRCORD2:
                    re2.setText("" + maxBean.getDriver_info().getDriver_name());
                    break;
                case ARRCORD3:
                    if (maxBean.getTrip_list().size() == 0) {
                        re3.setText("");
                        return;
                    }
                    re3.setText("已选择" + maxBean.getTrip_list().size() + "处参观");
                    break;
                case ARRCORD4:
                    re4.setText("" + maxBean.getRestaurant_info().getRes_name());
                    break;
                case ARRCORD5:
                    re5.setText("" + maxBean.getHostel_info().getHotel_name());
                    break;
                case ARRCORD6:
                    if ("".equals(maxBean.getRemark())) {
                        re6.setText("");
                    } else
                        re6.setText("已填写");
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
